package com.hui.controller;

import com.hui.config.Result;
import com.hui.entity.Book;
import com.hui.entity.User;
import com.hui.service.BookService;
import com.hui.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hui.controller
 * @Author: HUI
 * @CreateTime: 2021-12-06 11:47
 * @Description:
 */

@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {

    @Autowired
    BookService bookService;


    //分页查询数据
    @GetMapping
    public Result<?> queryBook(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String searchStr
    ) {

        return Result.success(bookService.queryBook(pageNum, pageSize, searchStr));
    }


    //新增书籍
    @PostMapping
    public Result<?> saveBook(@RequestBody Book book) {
//        int i = bookService.saveUser(book);
        return bookService.saveUser(book) > 0 ? Result.success(book.getBookName()) : Result.error("-1", "书籍已经存在");
      /*  log.info("实体信息：",book);
        System.out.println(book+"-----------------------");
        return Result.success();*/
    }

    //修改书籍信息
    @PutMapping
    public Result<?> updateUser(@RequestBody Book book) {
        int update = bookService.updateBook(book);
        return update > 0 ? Result.success(book.getBookName()) : Result.error("-1", book.getBookName() + "更新失败！");
    }

    //删除下架
    @DeleteMapping
    public Result<?> deleteBook(@RequestParam Integer id) {
//        log.info("参数："+id);
        return bookService.deleteBook(id) > 0 ? Result.success() : Result.error("-1", "下架失败");
//        return Result.success();
    }

    //批量删除
    @PostMapping("/bulkDeletion")
    public Result<?> bulkDeletion(@RequestBody List<Integer> ids){

        int state = bookService.bulkDeletion(ids);
        return state >0? Result.success():Result.error("-1","集合中长度为0");

    }

}
